package logic

import (
	"context"
	"encoding/json"
	"fmt"
	"manuscript-service/model/cache"
	"manuscript-service/model/mysql"
	"manuscript-service/servicerpc/internal/svc"
	"manuscript-service/servicerpc/servicerpc"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type FileListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewFileListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *FileListLogic {
	return &FileListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 列表查询
func (l *FileListLogic) FileList(in *servicerpc.FileListRequest) (*servicerpc.FileListResponse, error) {
	// todo: add your logic here and delete this line

	key := fmt.Sprintf("filesList_%s", in)

	result, _ := cache.Rdb.Get(cache.Ctx, key).Result()

	var filesList []*servicerpc.FileList

	if result == "" {
		filesList = mysql.FileList(mysql.DB, filesList)

		marshal, _ := json.Marshal(filesList)

		cache.Rdb.Set(cache.Ctx, key, marshal, time.Minute*120)
	} else {
		json.Unmarshal([]byte(result), &filesList)
	}

	return &servicerpc.FileListResponse{
		List: filesList,
	}, nil
}
